package com.qs.sckill.dao;

import com.qs.sckill.entity.SckillGoods;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface SckillGoodsDao {

    @Select("select * from t_sckill_goods where goods_id=#{goodsId}")
    SckillGoods getSckillGoodsByGoodsId(@Param("goodsId") String goodsId);

    /**
     * 秒杀商品减库存操作（注意：stock_count>0条件必须添加，数据库更新操作时会加锁，不会出现两个线程同时更新一条数据的情况，加上
     * 库存判断条件，可以解决秒杀商品超卖。）
     */
    @Update("update t_sckill_goods set stock_count=stock_count-1 where goods_id=#{goodsId} and stock_count>0")
    void reduceStock(SckillGoods sckillGoods);

    @Update("update t_sckill_goods set stock_count=#{stockCount} where goods_id=#{goodsId}")
    void updateStockCount(@Param("stockCount") Integer stockCount, @Param("goodsId") String goodsId);
}
